var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: '',
    identity: null,
    types: [],
    noticeList: [],
    info: {}
  },

  // 查看消息
  toNotice: function () {
    wx.navigateTo({
      url: '../../pages/notice/notice',
    })
  },

  // 外快分类
  toClassification: function () {
    wx.navigateTo({
      url: '/pages/classification/classification',
    })
  },

  // 输入框到外快列表
  inputToList: function (e) {
    wx.navigateTo({
      url: `/pages/extrasList/extrasList?type=${e.detail.value}`,
    })
  },

  // 分类到外快列表
  toList: function (e) {
    wx.navigateTo({
      url: `/pages/extrasList/extrasList?type=${e.currentTarget.dataset.item.wt_name}&typeId=${e.currentTarget.dataset.item.wr_id}`,
    })
  },

  // 外快详情
  toDetail: function (e) {
    wx.navigateTo({
      url: `/pages/extrasDetail/extrasDetail?wid=${e.currentTarget.dataset.wid}`,
    })
  },

  // 简历详情
  toResumeDetail: function (e) {
    wx.navigateTo({
      url: '/pages/resumeDetail/resumeDetail?rid=' + e.currentTarget.dataset.rid + '&type=index',
    })
  },

  // toLogin: function(e) {
  //   wx.navigateTo({
  //     url: '/pages/login/login',
  //   })
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    qqmapsdk = new QQMapWX({
      key: 'ZZ4BZ-GUXY3-KRL3R-YZL6A-MV6G6-5IB57' //这里自己的key秘钥进行填充
    });
    that.setData({
      identity: wx.getStorageSync('identity')
    })
    wx.showLoading({
      title: '正在加载...',
    })

    app.post('Common/workType').then(res => {
      let types = res.data.result_data
      that.setData({
        types
      })
    }).then(() => {
      app.post('Index/index', {
        token: wx.getStorageSync('token')
      }).then(res => {
        let obj = res.data.result_data
        obj.resume.forEach((item, index) => {
          let str = obj.resume[index].r_args2
          obj.resume[index].r_args2 = str.split(',')
        })
        that.setData({
          info: obj
        })
        wx.hideLoading()

      })
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let vm = this;
    vm.getUserLocation();

  },

  getUserLocation: function () {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        // console.log(JSON.stringify(res))
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      vm.getLocation();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          vm.getLocation();
        } else {
          //调用wx.getLocation的API
          vm.getLocation();
        }
      }
    })
  },
  // 微信获得经纬度
  getLocation: function () {
    let vm = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        // console.log(JSON.stringify(res))
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        vm.getLocal(latitude, longitude)
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },
  // 获取当前地理位置
  getLocal: function (latitude, longitude) {
    let vm = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        // console.log(JSON.stringify(res));
        // console.log(res.result.ad_info)
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        let district = res.result.ad_info.district
        let address = province + city + district
        wx.setStorageSync('province', province)
        wx.setStorageSync('city', city)
        wx.setStorageSync('district', district)
        vm.setData({
          address
        })
        wx.setNavigationBarTitle({
          title: address,
        })
        if (wx.getStorageSync('identity') == 1) {
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: '#284A6C',

          })
        } else {
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: '#DC9538',

          })
        }
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

function setTitle(identity) {

}